package com.scap.authen.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.scap.authen.entity.Role;
import com.scap.authen.entity.RoleApp;
import com.scap.authen.service.ApplicationService;
import com.scap.authen.service.RoleAppService;
import com.scap.authen.service.RoleService;
import com.scap.authen.service.SiteService;
import com.scap.authen.util.DataTable;

@Controller
@RequestMapping(value = "/roleApp")
public class RoleApplicationMappingController {	
	@Autowired
	private ApplicationService voApplicationService;	
	@Autowired
	private RoleService voRoleService;
	@Autowired
	SiteService voSiteService;
	@Autowired
	private RoleAppService voRoleAppService;
	@Autowired
	private ApplicationContext context;
	DataTable voDataTable = new DataTable();
	
	@RequestMapping( method = RequestMethod.GET)
	public String rolePage ( HttpServletRequest request ,ModelMap model, 
			@RequestParam String roleId, 
			@RequestParam String siteId, 
			@RequestParam String roleName) {
		//find Application checked
		String vaAppChecked = "";
		vaAppChecked = voRoleAppService.findApplicationChecked(roleId, siteId);		
		model.addAttribute("roleApp",new RoleApp());
		model.addAttribute("page", "RoleApplication Management");
		model.addAttribute("vaAppChecked", vaAppChecked);
		model.addAttribute("vaRoleId", roleId);
		model.addAttribute("vaSiteId", siteId);
		model.addAttribute("vaRoleName", roleName);
		return "Role/roleApplicationMapping";
	} 	
	
	@RequestMapping(value = "/roleAppDataTables", method = RequestMethod.GET)
	public @ResponseBody String springPaginationDataTables(HttpServletRequest request) throws IOException {	
		//find all Application
		List<Object> roleAppList = voApplicationService.getAllApplication();
		String searchParameter = request.getParameter("sSearch");
		roleAppList = voApplicationService.getListBasedOnSearchParameter(searchParameter, roleAppList);	
		return voDataTable.springPaginationDataTables(request,roleAppList);
	}
	
	@RequestMapping(method = RequestMethod.POST)
	public String roleApplicationMappingManagement ( @Valid Role role ,BindingResult result, HttpServletRequest request, ModelMap model ) {		
		String vaMessage = "";	
		String vaRoleId = request.getParameter("vaRoleId");
//		String vaRoleName = request.getParameter("vaRoleName");
		String vaSiteId = request.getParameter("vaSiteId");
		String vaApplicationId = request.getParameter("paApplicationId");		
		try {
			String voApplicationId[]= vaApplicationId.split(",");
			// update role status
			voRoleService.updateAppStatus(vaRoleId, vaSiteId);
			
			//Delete RoleApp by RoleId,SiteId
			voRoleAppService.deleteRoleAppByRoleIdSiteId(vaRoleId, vaSiteId);
			
			// insert RoleApp By vaApplicationId loop
			vaMessage = voRoleAppService.saveRoleApp(vaRoleId, vaSiteId, voApplicationId, request);	
			model.addAttribute("message", vaMessage);
		} catch (Exception e) {
			e.printStackTrace();
		}
		// find all site to drop down list
		List<Object> siteList = voSiteService.findAllSite();
		model.addAttribute("siteList",siteList);
		return  "Role/roleManagement";
	}
}
